"""
Case Type   : reverse函数
Case Name   : reverse()传入varchar类型的值,传入半/全角字符
Create At   : 2022/7/14
Owner       : @songjing20
Description :
    1.建表预置数据
    2.reverse()传入varchar类型的值,半角
    3.reverse()传入全角字符
    4.清理环境
Expect      :
    1.成功
    2.返回逆序的字符串
    3.返回逆序的字符串
    4.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class Sysfunction(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.commsh = CommonSH('dbuser')
        self.constant = Constant()
        self.tb_name = 't_ustore_reverse_0002'
        self.db_name = 'db_ustore_reverse_0002'
        create_db_res = self.commsh.execut_db_sql(
            f"create database {self.db_name} encoding 'utf8';")
        self.log.info(create_db_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_db_res,
                      f'预置utf8字符编码数据库失败')

    def test_regexp_substr(self):
        text = '-----step1:建表预置数据;expect:成功-----'
        self.log.info(text)
        sql = f'''drop table if exists {self.tb_name} cascade;
        create table {self.tb_name}(c_id int, c_char char(55), 
          c_varchar varchar(50)) with (storage_type = ustore);
        insert into {self.tb_name}(c_varchar) 
        values('abc123456789abc123456789abc123456789abc123456789'), 
          ('abc123456789abc123456789abc123456789a'), (null);'''
        sql_res = self.commsh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, sql_res,
                      f'建表失败:{text}')
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, sql_res,
                      f'插入数据失败:{text}')

        text = '-----step2:reverse()传入varchar类型的值,半角;expect:返回逆序的字符串-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"select c_varchar, reverse(c_varchar) from {self.tb_name} "
            f"where c_varchar is not null order by 1;", dbname=self.db_name)
        self.log.info(res)
        self.assertIn("abc123456789abc123456789abc123456789a            | "
                      "a987654321cba987654321cba987654321cba\n "
                      "abc123456789abc123456789abc123456789abc123456789 | "
                      "987654321cba987654321cba987654321cba987654321cba\n"
                      "(2 rows)", res, f'执行失败:{text}')

        text = '-----step3:reverse()传入全角字符;expect:返回逆序的字符串-----'
        self.log.info(text)
        res = self.commsh.execut_db_sql(
            f"select reverse('ａｂｃ')from {self.tb_name} "
            f"where c_varchar is not null order by 1;", dbname=self.db_name)
        self.log.info(res)
        self.assertIn("ｃｂａ\n ｃｂａ\n(2 rows)", res, f'执行失败:{text}')

    def tearDown(self):
        text = '-----step4:清理环境;expect:成功-----'
        self.log.info(text)
        drop_res = self.commsh.execut_db_sql(f"drop database {self.db_name};")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
